//该文件用于专门创建整个应用的路由器
import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);

//引入组件
import Login from "../views/Login.vue";
import Main from "../views/Main.vue";
import Home from "../views/Home.vue";
import MenuAdd from "../views/MenuAdd.vue";
import MenuSelect from "../views/MenuSelect.vue";
import UserAdd from "../views/UserAdd.vue";
import UserSelect from "../views/UserSelect.vue"
import SystemSettingUpdate from "../views/SystemSettingUpdate.vue";
import RoleSelect from "../views/RoleSelect.vue";
import SensorDetailSelect from "../views/SensorDetailSelect.vue";
import SensorTypeSelect from "../views/SensorTypeSelect.vue";
import SensorSendDataSelect from "../views/SensorSendDataSelect.vue";
import DataStatistics from "../views/DataStatistics.vue";
import AccessLog from "../views/AccessLog.vue";

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

// 创建并暴露一个路由器
const router = new VueRouter({
    // mode: 'hash',
    mode: 'history',
    routes: [{
        path: '/login',
        component: Login
    }, {
        path: '/main',
        component: Main,
        redirect: '/main/home',
        children: [{
            path: 'Home',
            component: Home
        }, {
            path: 'MenuAdd',
            component: MenuAdd
        }, {
            path: 'MenuSelect',
            component: MenuSelect
        }, {
            path: 'userAdd',
            component: UserAdd
        }, {
            path: 'userSelect',
            component: UserSelect
        }, {
            path: 'SystemSettingUpdate',
            component: SystemSettingUpdate
        }, {
            path: "RoleSelect",
            component: RoleSelect
        }, {
            path: "SensorDetailSelect",
            component: SensorDetailSelect
        }, {
            path: "SensorTypeSelect",
            component: SensorTypeSelect
        }, {
            path: "SensorSendDataSelect",
            component: SensorSendDataSelect
        }, {
            path: "DataStatistics",
            component: DataStatistics
        }, {
            path: "AccessLog",
            component: AccessLog
        }]
    }, {
        path: '*',
        redirect: '/login',
    }]
});
//全局前置路由守卫--初始化时调用，路由切换时调用
router.beforeEach((from, to, next) => {

    //若用户为登录，则对路径进行拦截（/login和/不拦截）
    var isLogin = localStorage.getItem("isLogin");
    if (isLogin === "1" || from.path === "/login" || from.path === "/") {
        next();
    }
})

//全局后置路由守卫--初始化时调用，路由切换后调用
router.afterEach((from, to) => {
    from == to;
})


export default router;